package com.AEWMS.Servlet.Score;

import com.AEWMS.Dao.ScoreWarningRank;
import com.AEWMS.Pojo.AdminScoreView;
import com.AEWMS.Service.CourseScoreService;
import com.AEWMS.Service.ScoreWarnRankService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "AdminScoreServlet", urlPatterns = "/AdminScoreServlet")
public class AdminScoreServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String year = request.getParameter("year");
        String type = request.getParameter("type");
        CourseScoreService css = new CourseScoreService();
        ScoreWarnRankService swrs = new ScoreWarnRankService();
        ArrayList<AdminScoreView> as = css.findAllStudentScoreRankByyt(year,type);
        ArrayList<ScoreWarningRank> ss = swrs.findAllScoreWarningRankNotView();
        // 暂存变量，用户保存处理后的结果
        ArrayList<AdminScoreView> temp = new ArrayList<>();
        // 循环每一条数据，处理预警等级
        for (AdminScoreView a : as){
            if (a.getBxFail() >= ss.get(0).getWarningStandardStart()) {
                a.setRank(ss.get(0).getWarningRank());
                break;
            }
            else {
                Double t = a.getBxFail() + a.getXxFail();
                for (ScoreWarningRank s : ss) {
                    if (t >= s.getWarningStandardStart() && t < s.getWarningStandardEnd()) {
                        a.setRank(s.getWarningRank());
                        break;
                    }
                }
            }
            temp.add(a);
        }
        response.getWriter().println(JSON.toJSONString(temp));
        css.closeCon();
        swrs.closeCon();
    }
}